const Service = require("uni-cloud-router").Service;
module.exports = class UserService extends Service{
	async allotWorkerInProject({projectID,worker_ids}){
		let allotWorkerRes = await this.db.collection('project_store').where({
			_id:projectID
		}).update({
			worker_ids
		})
		
		if(allotWorkerRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"分配成功"
			}
		} else {
			return {
				status:0,
				msg:"分配失败"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async updateWorkerStatus({status,wkID}){
		let updateStatusRes = await this.db.collection('userdata').where({
			wkID
		}).update({
			workStatus:status
		})
		
		if(updateStatusRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"添加成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async deleteProjectInWorker({projectID,wkID}){
		let res = await this.db.collection('project_store').where({
			_id:projectID
		})
		.field({'worker_ids':true})
		.get()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			let wkIDs = res.data[0].worker_ids;
			
			let updateWKinfo = {};
			wkIDs.forEach(item => {
				if(item.wkID == wkID){
					updateWKinfo.wkID = item.wkID;
					updateWKinfo.salaryWay = item.salaryWay;
					updateWKinfo.drillDeepCount = item.drillDeepCount;
					updateWKinfo.signCountDay = item.signCountDay;
				}
			})
			let deleteProjectID = res.data[0]._id;
			//把当前需要更改的员工存入历史项目信息表(存储该员工曾经在该项目里面工作过,后来换项目了)
			updateWKinfo.projectID = deleteProjectID;
			let addWKhistoryProjecyRes = await this.db.collection('wk_history_project').add(updateWKinfo);
			let deleteIndex = wkIDs.findIndex(item=>item.wkID===wkID);
			let deleteRes = wkIDs.splice(deleteIndex,1);
			
			let deleteWKIDRes = await this.db.collection('project_store').where({
				_id:projectID
			}).update({
				worker_ids:wkIDs
			})
			
			if(deleteWKIDRes.affectedDocs !== 0){
				return {
					status:0,
					msg:"删除成功"
				}
			} else {
				return {
					status:1,
					msg:"稍后重试"
				}
			}
		} else {
			return {
				status:1,
				msg:"没有此项目"
			}
		}
		
		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	async selectOtherProjectWorker({projectID,IDandSalaryWay}){
		const dbCmd = this.db.command;
		let queryProjectInWorker = await this.db.collection('project_store').where({
			worker_ids:dbCmd.elemMatch({	
				wkID:IDandSalaryWay.wkID
			})
		}).get()
		
		if(queryProjectInWorker.affectedDocs !== 0){
			let wkIDs = queryProjectInWorker.data[0].worker_ids;
			let updateWKinfo = {};
			wkIDs.forEach(item => {
				if(item.wkID == IDandSalaryWay.wkID){
					updateWKinfo.wkID = item.wkID;
					updateWKinfo.salaryWay = item.salaryWay;
					updateWKinfo.drillDeepCount = item.drillDeepCount;
					updateWKinfo.signCountDay = item.signCountDay;
				}
			})
			
			let deleteIDIndex = wkIDs.findIndex(item => item.wkID == IDandSalaryWay.wkID);
			let afterDeleteRes = wkIDs.splice(deleteIDIndex,1);
			let deleteProjectID = queryProjectInWorker.data[0]._id;
			//把当前需要更改的员工存入历史项目信息表(存储该员工曾经在该项目里面工作过后来换项目了)
			updateWKinfo.projectID = deleteProjectID;
			let addWKhistoryProjecyRes = await this.db.collection('wk_history_project').add(updateWKinfo);
			
			//调用云函数更改
			let updateProjectRes = await uniCloud.callFunction({
				name:"main",
				data:{
					action:"allotWKAdmin/deleteProjectInWorker",
					data:{
						projectID:deleteProjectID,
						worker_ids:wkIDs
					}
				}
			})
			
			if(updateProjectRes.result.status === 0){
				return {
					status:0,
					msg:"修改成功"
				}
			} else {
				return {
					status:1,
					msg:"稍后重试"
				}
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getUserData(){
		let queryUserDatas = await this.db.collection('userdata').get();
		
		if(queryUserDatas.affectedDocs !== 0 || queryUserDatas.data.length !== 0){
			return {
				status:0,
				data:queryUserDatas.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getProjectWKID({projectID}){
		const dbCmd = this.db.command;
		let workerIDs = await this.db.collection("project_store")
		.field({'worker_ids':true})
		.where({_id:dbCmd.eq(projectID)})
		.get()
		
		if(workerIDs.affectedDocs !== 0 || workerIDs.data.length !== 0){
			return{
				status:0,
				msg:"获取成功",
				data:workerIDs.data
			}
		} else{
			return {
				status:1,
				msg:"没有员工"
			};
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getUserDataByStatus(workStatus,wkID){
		let getDataByStatus = await this.db.collection('userdata').where({workStatus,wkID}).get();
		if(getDataByStatus.affectedDocs !== 0 || getDataByStatus.data.length !== 0){
			return{
				status:0,
				msg:"获取成功",
				data:getDataByStatus.data
			}
		} else{
			return {
				status:1,
				msg:"没有员工"
			};
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getUserDataByCompanyID(workStatus,pm_company){
		let wkInfoRes = await this.db.collection("userdata").where({
			workStatus,pm_company
		}).get();
		
		if(wkInfoRes.affectedDocs !== 0 || wkInfoRes.data.length !== 0){
			return{
				status:0,
				msg:"获取成功",
				data:wkInfoRes.data
			}
		} else{
			return {
				status:1,
				msg:"没有员工"
			};
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}